package com.mayikt.main.consumer;

import com.mayikt.main.api.impl.mapper.SysUserLoginLogMapper;
import com.mayikt.main.manage.LoginLogManage;
import com.mayikt.main.producer.UniqueLogProducer;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author 余胜军
 * @ClassName UniqueLoginConsumer
 * @qq 644064779
 * @addres www.mayikt.com
 * 微信:yushengjun644
 */
@Component
@RabbitListener(queues = "fanout_uniquelogin_queue")
@Slf4j
public class UniqueLoginConsumer {

    @Autowired
    private LoginLogManage loginLogManage;
    @Autowired
    private SysUserLoginLogMapper sysUserLoginLogMapper;
    @Autowired
    private UniqueLogProducer uniqueLogProducer;

    /**
     * @return void
     * @Author wangjin
     * @Description //消费者如果消费失败的情况下默认会无限的重试
     * @Date 2022/5/14 0014
     * @Param [msg, message, channel]
     **/
    @RabbitHandler
    public void process(String msg, Message message, Channel channel) throws IOException {
        //   try {
      /*  log.info(">>使用MQ异步的形式记录日志:{}<<", msg);
        if (StringUtils.isEmpty(msg)) {
            return;
        }
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        // 解析json
        SysUserLoginLog sysUserLoginLog = JSONObject.parseObject(msg, SysUserLoginLog.class);
        //不管mq重试msid是不会发生变化的
        String messageId = sysUserLoginLog.getMessageId();
        //根据msid查找redis是否已经达到上限次数
        String redisRetryCount = RedisUtil.getString(messageId);
        Integer retryCount = redisRetryCount == null ? 0 : Integer.parseInt(redisRetryCount);
        RedisUtil.setString(messageId, (retryCount + 1) + "");
        if (retryCount >= MQConstant.BUFFER_SIZE) {
            uniqueLogProducer.sendMsg(msg);
            channel.basicAck(deliveryTag, true);
            return;
        }
        int i = 1 / 0;
        //如果根据messageid去数据库里可以查询到数据就不用再去插入
        if (Strings.isEmpty(messageId)) {
            //
            channel.basicAck(deliveryTag, true);
            return;
        }
        //如果代码有问题再怎么重试都没有用，限制重试多次的情况下，未来避免消息一直堆积mq服务器
        //先认为mq是消费成功的，然后转移到死信队列
        //如果根据messageId 查询到对应的记录，如果存在的话就不能够重复执行业务，手动ack牵手将msg消息删除
        QueryWrapper<SysUserLoginLog> sysUserLoginLogQueryWrapper = new QueryWrapper<>();
        sysUserLoginLogQueryWrapper.eq("message_id", messageId);
        SysUserLoginLog userLoginLog = sysUserLoginLogMapper.selectOne(sysUserLoginLogQueryWrapper);
        if (userLoginLog != null) {
            //该数据已经在db里存在了，不需要重复插入，手动签收就可以
            channel.basicAck(deliveryTag, true);
            return;
        }
        //说明业务没有执行完继续执行业务代码
        boolean flags = loginLogManage.asynLoginLog(sysUserLoginLog);
        if (!flags) {
            channel.basicNack(deliveryTag, true, true);
            return;
        }

        channel.basicAck(deliveryTag, true);
        return;*/
        // 该方法没有报错的情况下 mq服务器端是不会给该消费者继续重试

       /* } catch (Exception e) {
            log.error("e:{}", e);
        }*/
    }
}
